#! /usr/bin/env python
# -*- coding: utf-8 -*-


import os
import sys
import random
import math

import pygame
from pygame.locals import *

from sys import exit

from layout import VLayout
from maze import Maze, Person
from road import Road
from wall import Wall
from pygame_gui import Button


def test():
    print "test"


if __name__ == "__main__":

    # 初始化pygame,为使用硬件做准备
    pygame.init()

    # 创建了一个窗口
    SCREEN = pygame.display.set_mode((1000, 600), pygame.NOFRAME, 32)
    SCREEN.fill(pygame.Color('#CCCCCC'))

    BTN_FONT_SIZE = 20
    BTN_FONT = pygame.font.SysFont('Monospace', BTN_FONT_SIZE, True)
    PERSON_FONT = pygame.font.SysFont('Monospace', 15, True)

    Person.init(PERSON_FONT)

    BTN_COLOR = pygame.Color('#0099CC')

    WALL_WIDTH = 4  # 墙角是会重叠的
    WALL_HEIGHT = 40

    ROAD_WIDTH = WALL_HEIGHT - 2*WALL_WIDTH
    ROAD_HEIGHT = ROAD_WIDTH

    WALL = Wall(WALL_WIDTH, WALL_HEIGHT, SCREEN)
    ROAD = Road(ROAD_WIDTH, ROAD_HEIGHT, SCREEN)

    maze_width = 800
    gui_width = 200

    # 设置左边的按钮布局，初始化右边的迷宫
    vertical_layout = VLayout(pygame.Rect(0, 20, gui_width, 500))

    genrate_btn_rect = vertical_layout.generate_rect(gui_width, 30)
    generate_btn = Button(SCREEN, 'Generate_DFS', genrate_btn_rect, BTN_FONT, BTN_COLOR)

    play_btn_rect = vertical_layout.generate_rect(gui_width, 30)
    play_btn = Button(SCREEN, 'Play_DFS', play_btn_rect, BTN_FONT, BTN_COLOR)

    astar_btn_rect = vertical_layout.generate_rect(gui_width, 30)
    astar_btn = Button(SCREEN, 'Play_ASTAR', astar_btn_rect, BTN_FONT, BTN_COLOR) 

    maze = Maze(200, 0, maze_width, 600, SCREEN, WALL, ROAD)

    # 设置窗口标题
    pygame.display.set_caption("Maze")

    clock = pygame.time.Clock()
    
    # 游戏主循环
    while True:
     
        for event in pygame.event.get():
            # 接收到退出事件后退出程序
            if event.type == QUIT:
                exit()

            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    exit()

            if event.type == MOUSEBUTTONDOWN:
                """鼠标点击事件
                """
                mouse_pos = event.pos
                mouse_button = event.button

                generate_btn.connect_mousedownevent_with(maze.generate_maze_by_using_dfs)
                play_btn.connect_mousedownevent_with(maze.hack_maze_by_using_dfs)
                astar_btn.connect_mousedownevent_with(maze.hack_maze_by_using_astar)

            if event.type == MOUSEMOTION:
                Button.register_mousemotion_event(event)

        # 将背景图画上去
     
        x, y = pygame.mouse.get_pos()
     
        # 刷新一下画面
        clock.tick(10)
        pygame.display.update()
